package com.yuexin.project.store.outbound.controller;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuexin.common.utils.DateUtils;
import com.yuexin.common.utils.poi.ExcelUtil;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.framework.aspectj.lang.annotation.Log;
import com.yuexin.framework.aspectj.lang.enums.BusinessType;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.domain.AjaxResult;
import com.yuexin.framework.web.page.TableDataInfo;
import com.yuexin.project.store.goods.service.IGoodsListService;
import com.yuexin.project.store.outbound.domain.Outbound;
import com.yuexin.project.store.outbound.service.IOutboundService;

/**
 * 出库单 信息操作处理
 * 
 * @author yuexin
 * @date 2018-11-30
 */
@Controller
@RequestMapping("/store/outbound")
public class OutboundController extends BaseController
{
    private String prefix = "store/outbound";
	
	@Autowired
	private IOutboundService outboundService;
	
	@Autowired
	private IGoodsListService goodsListService;
	
	@RequiresPermissions("store:outbound:view")
	@GetMapping()
	public String outbound()
	{
	    return prefix + "/outbound";
	}
	
	/**
	 * 查询出库单列表
	 */
	@RequiresPermissions("store:outbound:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Outbound outbound)
	{
		startPage();
		outbound.setCompanyId(ShiroUtils.getCompanyId());
        List<Outbound> list = outboundService.selectOutboundList(outbound);
		return getDataTable(list);
	}
	
	/**
	 * 新增出库单
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		mmap.put("goodsLists", goodsListService.selectGoodsListAll(ShiroUtils.getCompanyId()));
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存出库单
	 */
	@RequiresPermissions("store:outbound:add")
	@Log(title = "出库单", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Outbound outbound)
	{		
		outbound.setCompanyId(ShiroUtils.getCompanyId());
		return toAjax(outboundService.insertOutbound(outbound));
	}

	private boolean isThisMonth(Date storeDate) {
		boolean result = false;
		String dateStr = DateUtils.getDate();
		String storeDateStr = DateUtils.dateTime(storeDate);
		String MM=dateStr.substring(5, 7);
		String mm=storeDateStr.substring(5, 7);
		if(MM.equals(mm)) {
			result = true;
		}
		return result;
	}
	
	/**
	 * 修改出库单
	 */
	@GetMapping("/edit/{outboundId}")
	public String edit(@PathVariable("outboundId") Integer outboundId, ModelMap mmap)
	{
		Outbound outbound = outboundService.selectOutboundById(outboundId);
		mmap.put("outbound", outbound);
		mmap.put("goodsLists", goodsListService.selectGoodsListAll(ShiroUtils.getCompanyId()));
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存出库单
	 */
	@RequiresPermissions("store:outbound:edit")
	@Log(title = "出库单", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Outbound outbound)
	{		
		Date outboundDate = outbound.getOutboundDate();
		if (!isThisMonth(outboundDate))
	    {
	        return error(1, "只能修改当月的出库单");
	    }
		return toAjax(outboundService.updateOutbound(outbound));
	}
	
	/**
	 * 删除出库单
	 */
	@RequiresPermissions("store:outbound:remove")
	@Log(title = "出库单", businessType = BusinessType.DELETE)
	@PostMapping( "/remove/{outboundId}")
	@ResponseBody
	public AjaxResult remove(@PathVariable("outboundId") Integer outboundId)
	{		
		Outbound outbound = outboundService.selectOutboundById(outboundId);
		Date outboundDate = outbound.getOutboundDate();
		if (!isThisMonth(outboundDate))
	    {
	        return error(1, "只能删除当月的出库单");
	    }
		return toAjax(outboundService.deleteOutboundById(outboundId));
	}
	
	/**
	 * 导出 
	 */
    @RequiresPermissions("store:outbound:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Outbound outbound)
    {
		outbound.setCompanyId(ShiroUtils.getCompanyId());
        List<Outbound> list = outboundService.selectOutboundList(outbound);
        for(Outbound i : list){
        	Date outboundDate = i.getOutboundDate();
        	String outboundDateExcel = DateUtils.dateTime(outboundDate);
        	i.setOutboundDateExcel(outboundDateExcel);
        }
        ExcelUtil<Outbound> util = new ExcelUtil<Outbound>(Outbound.class);
        return util.exportExcel(list, "Outbound");
    }
	
}
